﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using UnityEngine;

namespace Assets.Scripts.DL.ComponentScripts {
	public class WindWall : MonoBehaviour {
		[SerializeField] float velocity = 2;
		[SerializeField] SpriteRenderer sp;
		public void Init(Vector3 pos, int dir) {
			transform.localScale = new Vector3(dir, 1, 1);
			transform.position = pos;
			velocity *= dir;
			StartCoroutine(Move());
			var c = sp.color;
			c.a = 1;
			sp.color = c;
		}

		public void Update() {
			transform.position += new Vector3(velocity * Time.deltaTime, 0, 0);
		}

		IEnumerator Move() {
			Timer timer = new Timer();
			while (timer.Elapsed < 1) {
				yield return null;
			}
			velocity = 0;
			yield return new WaitForSeconds(2);
			while (sp.color.a > 0.1f) {
				var c = sp.color;
				c.a = Mathf.Lerp(c.a, 0, 0.01f);
				sp.color = c;
				yield return null;
			}
			ObjectPool.Instance.RecycleObject(ObjPoolStr.WindWall, gameObject);
		}

		private void OnTriggerEnter2D(Collider2D collision) {
			if (collision.CompareTag(Tags.PlayerRemoteAtk)) {
				//Debug.Log("DefendOK");
				ObjectPool.Instance.RecycleObject(ObjPoolStr.Arrow, collision.gameObject);
			}
		}
	}
}
